#!/bin/bash

[ -f Makefile ] && ./distclean.sh

TARGET=linux

PREFIX="${HOME}/dani"
DD_HOSTS="p4 anb anb1 render mms box mazsola v3"
PERF_FLAG=""
OPT_FLAG="-O2"

while [ -n "$1" ]; do
    case "$1" in
	-p)
	    PERF_FLAG="-pg"
	    ;;
	-d)
	    OPT_FLAG="-O0 -DDEVEL"
	    ;;
	-w)
	    PREFIX=${HOME}/prj/wucsim/inst
	    ;;
	[lLxX]*) TARGET=linux;;
	[mMwW]6*) TARGET=w64;;
	[mMwW]*) TARGET=mingw;;
	*) echo "Unknown target \"${1}\"" >&2; exit 1;;
    esac
    shift
done

#if [ -f devel ]; then
#    export CXXFLAGS="-O0"
#    export CFLAGS="-g -O0"
#fi

export CXXFLAGS="-g $OPT_FLAG $PERF_FLAG"
export CFLAGS="-g $OPT_FLAG $PERF_FLAG"

#if [ -n "$PERF_FLAG" ]; then
#    CXXFLAGS="${CXXFLAGS} ${PERF_FLAG}"
#    CFLAGS="${CFLAGS} ${PERF_FLAG}"
#fi

case $TARGET in
    linux|l)
	if echo $DD_HOSTS|grep $(hostname) >/dev/null 2>&1 ; then
	    :
	fi
	./configure --prefix=${PREFIX} \
		    --disable-dlso \
		    --enable-ucsim \
		    "$@"
	#--enable-ucsim
	;;
    mingw|win|w)
	if [ -d	/usr/local/cross-tools/mingw32 ] ; then
	    export CC=/usr/local/cross-tools/mingw32/bin/gcc
	    export CXX=/usr/local/cross-tools/mingw32/bin/g++
	elif [ $(hostname) = "d30" ]; then
	    export CC=i586-mingw32msvc-gcc
	    export CXX=i586-mingw32msvc-g++
	    export HOST_OPT='--host=i586-mingw32msvc'
	    export LDFLAGS='-static-libgcc -static-libstdc++'	    
	else
	#if echo $DD_HOSTS|grep $(hostname) >/dev/null 2>&1; then
	    export CC=i686-w64-mingw32-gcc
	    export CXX=i686-w64-mingw32-g++
	    export HOST_OPT='--host=i686-w64-mingw32'
	    export LDFLAGS='-static-libgcc -static-libstdc++'
	fi
	echo CC=$CC CXX=$CXX HOST=$HOST_OPT
	./configure --prefix=${PREFIX} \
		    --disable-dlso \
		    --enable-ucsim \
		    $HOST_OPT "$@"
	#--enable-ucsim
	;;
    w5)
	export CC=i586-mingw32msvc-gcc
	export CXX=i586-mingw32msvc-g++
	export HOST_OPT='--host=i586-mingw32msvc'
	export LDFLAGS='-static-libgcc -static-libstdc++'	    
	echo CC=$CC CXX=$CXX HOST=$HOST_OPT
	./configure --prefix=${PREFIX} \
		    --disable-dlso \
		    --enable-ucsim \
		    $HOST_OPT "$@"
	;;	
    w64)
	export CC=x86_64-w64-mingw32-gcc
	export CXX=x86_64-w64-mingw32-g++
	export HOST_OPT='--host=x86_64-w64-mingw32'
	export LDFLAGS='-static-libgcc -static-libstdc++'
	echo CC=$CC CXX=$CXX HOST=$HOST_OPT
	./configure --prefix=${PREFIX} \
		    --disable-dlso \
		    --enable-ucsim \
		    $HOST_OPT "$@"
	#--enable-ucsim
	;;
    *)
	echo >&2 "Do not know how to configure!"
	;;
esac

for d in s51 m6809 stm8 tlcs z80; do
    [ -L ${d}.src/dtest ] || ln -s ../../test/$d ${d}.src/dtest
done

# End of conf
